package com.sip.stream.utils.zfy.osd;

import android.graphics.Rect;
import android.util.Log;

import com.sip.stream.utils.zfy.config.ZFYOSDConfig;

import java.util.Iterator;

public abstract class AbstractYUVOSDTool extends AbstractBaseOSDTool {
    private String cacheLastTimeStamp = null;
    public int POSTION_TOP_START = 1;
    public int POSTION_TOP_END = 2;
    public int POSTION_BOTTOM_START = 3;
    public int POSTION_BOTTOM_END = 4;

    protected abstract void addOSDToI420ASCII(byte[] bArr, OSDBean oSDBean);

    protected abstract void addOSDToI420Jni(byte[] bArr, OSDBean oSDBean);

    public AbstractYUVOSDTool(int width, int height, ZFYOSDConfig zFYOSDConfig) {
        this.cacheSrcWidth = width;
        this.cacheSrcHeight = height;
        this.cacheScale = getScale(width);
        readConfigs(zFYOSDConfig);
        Log.i(TAG, "srcWidth " + width + " srcHeight " + height + " scale" + this.cacheScale);
    }

    public boolean addOSDCache(byte[] bArr) {
        String timeStampStr = getTimeStampStr();
        if (!timeStampStr.equalsIgnoreCase(this.cacheLastTimeStamp)) {
            this.cacheLastTimeStamp = timeStampStr;
            setupDynamicOSD(bArr, timeStampStr);
        }
        Iterator<OSDBean> it = this.staticOSDBeans.iterator();
        while (it.hasNext()) {
            addOSDToI420Jni(bArr, it.next());
        }
        Iterator<OSDBean> it2 = this.dynamicOSDBeans.iterator();
        while (it2.hasNext()) {
            addOSDToI420ASCII(bArr, it2.next());
        }
        return true;
    }

    public byte[] getDynamicOSD(OSDBean oSDBean, String str) {
        int dynamicOsdType = oSDBean.getDynamicOsdType();
        if (dynamicOsdType == 1) {
            str = getGPSStr();
        } else if (dynamicOsdType != 2) {
            str = dynamicOsdType != 3 ? null : getDeviceStatusStr();
        }
        return str == null ? new byte[0] : str.getBytes();
    }

    protected void setupDynamicOSD(byte[] bArr, String str) {
        Iterator<OSDBean> it = this.dynamicOSDBeans.iterator();
        while (it.hasNext()) {
            OSDBean next = it.next();
            next.setOsdData(getDynamicOSD(next, str));
        }
    }

    protected Rect getASCIIRect(int i, int i2, int i3, int i4, int i5, int i6) {
        int i7 = i == this.POSTION_TOP_START ? (i2 - 15) - ((i4 * 8) * i6) : 15;
        if (i == this.POSTION_TOP_END) {
            i7 = (i2 - 15) - ((i4 * 8) * i6);
        }
        int i8 = i == this.POSTION_BOTTOM_START ? (this.cacheSrcHeight - 15) - ((this.cacheScale * 16) * i5) : 15;
        if (i == this.POSTION_BOTTOM_END) {
            i7 = (i2 - 15) - ((i4 * 8) * i6);
        }
        return new Rect(i7, i8, i2, i3);
    }

    public void printOSD(byte[] bArr) {
        int length = bArr.length;
        int i = length / 16;
        StringBuilder sb = new StringBuilder();
        for (int i2 = 0; i2 < length; i2++) {
            byte b = bArr[i2];
            if (i2 % i == 0) {
                sb.append("\t\n");
            }
            for (int i3 = 0; i3 < 8; i3++) {
                if (((b << i3) & 128) == 0) {
                    sb.append(" ");
                } else {
                    sb.append("█");
                }
            }
        }
        Log.i(TAG, sb.toString());
    }
}
